package com.pc.web.goods;

import java.io.File;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.pc.web.HomeService;

@Controller
public class GoodsController {
	@Autowired
	private GoodsDao dao;
	@Autowired
	private HomeService homeService;
	private GoodsService goodsService;
	

	@RequestMapping(value = "/goods/form", method = RequestMethod.GET)
	// form이동
	public String form(Model model) {
		System.out.println("goods진입");
		homeService.categoryList(model);
		return "/goods/goods_form";
	}

	// 상품등록
	@RequestMapping(value = "/goods/insert", method = RequestMethod.POST)	
	public String insert(GoodsBeans bean, Map<String, Object> fileList,Model model) throws IOException {
		// 이미지 등록
		System.out.println("insert진입");

		List<MultipartFile> file=(List<MultipartFile>) bean.getFile();
		
		for(int i=0; i<file.size(); i++){

			String originName = file.get(i).getOriginalFilename().replaceAll(" ", "_");
			if(file.get(i).getOriginalFilename().isEmpty()){
				switch(i){
				case 0: bean.setGoods_image1("");break;
				case 1: bean.setGoods_image2("");break;
				case 2: bean.setGoods_image3("");break;
				case 3: bean.setGoods_image4("");break;
				case 4: bean.setGoods_image5("");break;
				case 5: bean.setGoods_image6("");break;
				case 6: bean.setGoods_image7("");break;
				case 7: bean.setGoods_image8("");break;
				case 8: bean.setGoods_image9("");break;
				case 9: bean.setGoods_image10("");break;
				case 10: bean.setGoods_image11("");break;
				case 11: bean.setGoods_image12("");break;				
				}
			}
			else{
				String extend = originName.substring(originName.lastIndexOf(".") - 1,originName.length());
				String imageName =(i)+System.currentTimeMillis() + file.size()+ extend;
				String path = "C:/Program Files/Project/pcmaster/pcmaster/src/main/webapp/resources/upload";
	
				// 1375780497293플   				DB
				// 11375780416512플.jpg			서버
				
				fileList.put(imageName, imageName);	
				
				try {
					MultipartFile files = file.get(i);
					File f = new File(path + "/" + imageName);
					files.transferTo(f);
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				switch(i){
				case 0: bean.setGoods_image1(imageName);break;
				case 1: bean.setGoods_image2(imageName);break;
				case 2: bean.setGoods_image3(imageName);break;
				case 3: bean.setGoods_image4(imageName);break;
				case 4: bean.setGoods_image5(imageName);break;
				case 5: bean.setGoods_image6(imageName);break;
				case 6: bean.setGoods_image7(imageName);break;
				case 7: bean.setGoods_image8(imageName);break;
				case 8: bean.setGoods_image9(imageName);break;
				case 9: bean.setGoods_image10(imageName);break;
				case 10: bean.setGoods_image11(imageName);break;
				case 11: bean.setGoods_image12(imageName);break;	
			}
		}

	}
		dao.insert(bean);		
		return this.read(bean, model);
	
	}

	// 홈이동
	@RequestMapping(value = "/goods/home", method = RequestMethod.GET)
	public String home() {
		System.out.println("goodshome진입");
		return "/goods/goods_home";
	}

	//상품 리스트	
	@RequestMapping(value = "/goods/list", method = RequestMethod.GET)
	public String read(GoodsBeans bean, Model model) {
		System.out.println("Goods list진입");
/*		goodsService.read(model);*/

		List<GoodsBeans> list=dao.list();//리스트를 배열로
		System.out.println("리스트 사이즈 : "+list.size());
		model.addAttribute("list",list);
		return "/goods/goods_list";

	}
	
	//상품상세보기
	@RequestMapping(value = "/goods/detail", method = RequestMethod.GET)
	public String detail(GoodsBeans beans, Model model, String goods_code) {
		System.out.println("디테일 진입");
		System.out.println(goods_code);
		beans = dao.detail(goods_code);

		System.out.println("상품코드 : " + beans.getGoods_code());
		System.out.println("상품명 : " + beans.getGoods_name());
		System.out.println("재고수량 : " + beans.getGoods_count());
		System.out.println("카테고리 : " + beans.getGoods_category());
		System.out.println("상품정보 : " + beans.getGoods_information());
		
		model.addAttribute("beans", beans);
		return "/goods/goods_detail";
	}
	
	//관리자 상품 업데이트 페이지
	@RequestMapping(value="/goods/updateForm", method=RequestMethod.GET)
	public String update(GoodsBeans beans,Model model,String GOODS_CODE){						
		beans=dao.updateForm(GOODS_CODE);//리턴을 빈즈에 받아서 처리함
		model.addAttribute("beans", beans);
		return "/goods/goods_update";
	}
/*	
	//관리자 상품 업데이트 
	@RequestMapping(value="/goods/update", method=RequestMethod.POST)
	public String updateForm(GoodsBeans beans,Model model){
		dao.update(beans);
		System.out.println("업데이트진입");
		msg="update";
		model.addAttribute("msg", msg);
		return "/goods/goods_msg";
	}
	
	//관리자 상품 삭제
	@RequestMapping(value="/goods/delete", method=RequestMethod.GET)
	public String delete(GoodsBeans beans,Model model){
		dao.delete(beans);
		msg="delete";
		model.addAttribute("msg", msg);
		return "/goods/goods_msg";
	}*/
}
